package interface_USB;

public class Computer {//在Computer内使用KeyBoard和Mouse两个类
    public void powerOn() {
        System.out.println("打开笔记本电脑");
    }
    public void powerOff() {
        System.out.println("关闭笔记本电脑");
    }

    public void useDevice(USB usb) { //向上转型
        usb.openDevice(); //无论是鼠标还是键盘都需要打开和关闭，因此放在if-else外
        if(usb instanceof Mouse) {
            Mouse mouse = (Mouse) usb;//向下转型
            mouse.click();
        }else if(usb instanceof KeyBoard) {
            KeyBoard keyBoard = (KeyBoard) usb;
            keyBoard.input();
        }
        usb.closeDevice();
    }
}
